#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using System.Text;
using System.Reflection;
using System.IO;

using KLib.Physics;
#endregion

namespace KLib.Utilities
{
    public class ContentExporter
    {
        #region Fields

        #endregion

        #region Properties

        #endregion

        #region Initialization

        public ContentExporter()
        {

        }

        #endregion

        #region Methods
        public static void ExportContent<T>(T asset) where T : Entities.Entity
        {
            StreamWriter writer = new StreamWriter(asset.Name + ".ent");
            PropertyInfo[] properties = asset.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);

            foreach (PropertyInfo property in properties)
            {
                object[] attr = property.GetCustomAttributes(typeof(Microsoft.Xna.Framework.Content.ContentSerializerIgnoreAttribute), false);
                if (attr.Length > 0) continue;

                if (property.PropertyType == typeof(string)
                    || property.PropertyType == typeof(int)
                    || property.PropertyType == typeof(float)
                    || property.PropertyType == typeof(bool))
                    writer.WriteLine(property.Name + ":" + property.GetValue(asset, null));
                else if (property.PropertyType == typeof(RigidBody))
                    writer.Write(asset.PhysicsBody.SaveString(property.Name));
                else if (property.PropertyType == typeof(Color))
                {
                    Color col = (Color)property.GetValue(asset, null);
                    writer.WriteLine(property.Name + ":" + col.R + "," + col.G + "," + col.B + "," + col.A);
                }
                else if (property.PropertyType == typeof(Vector2))
                {
                    Vector2 vect = (Vector2)property.GetValue(asset, null);
                    writer.WriteLine(property.Name + ":" + vect.X + "," + vect.Y);
                }
            }
            writer.Close();
        }

        public static string GetExportContentString<T>(T asset, string leadingPropertyString) where T : Physics.Collisions.Geom
        {
            StringBuilder str = new StringBuilder();
            PropertyInfo[] properties = asset.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy);

            foreach (PropertyInfo property in properties)
            {
                object[] attr = property.GetCustomAttributes(typeof(Microsoft.Xna.Framework.Content.ContentSerializerIgnoreAttribute), false);
                if (attr.Length > 0
                    || !property.CanWrite) continue;

                if (property.PropertyType == typeof(string)
                    || property.PropertyType == typeof(int)
                    || property.PropertyType == typeof(float)
                    || property.PropertyType == typeof(bool))
                    str.AppendLine(leadingPropertyString + ":" + property.Name + ":" + property.GetValue(asset, null).ToString());
                else if (property.PropertyType == typeof(Vector2))
                {
                    Vector2 vect = (Vector2)property.GetValue(asset, null);
                    str.AppendLine(leadingPropertyString + ":" + property.Name + ":" + vect.X + "," + vect.Y);
                }
                else if (property.PropertyType == typeof(Physics.Collisions.Vertices))
                {
                    Physics.Collisions.Vertices newVerts = (KLib.Physics.Collisions.Vertices)property.GetValue(asset,null);
                    str.Append(leadingPropertyString + ":" + property.Name + ":");
                    for (int i = 0; i < newVerts.Count; i++)
                    {
                        str.Append("(" + newVerts[i].X + "," + newVerts[i].Y + ")");
                    }
                    str.AppendLine();
                }
            }
            return str.ToString();
        }
        #endregion

        #region Helper Methods

        #endregion
    }
}
